
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';
import '../../../utils/commonUtil.dart';
import 'package:permission_handler/permission_handler.dart';

class MyGallery extends StatefulWidget {
  final List<String> imageList;
  final int index;
  const MyGallery( {super.key, required this.imageList, required this.index});

  @override
  State<StatefulWidget> createState() => _MyGallery();

}

class _MyGallery extends State<MyGallery>{
  int _currentIndex = 0;
  final GlobalKey _globalKey = GlobalKey();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // 检查并请求权限
  /*  var status = await Permission.storage.status;
    print("permission status is " + status.toString());

    // Request storage permission
    await Permission.storage.request();*/
    setState(() {
      _currentIndex = widget.index;
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Stack(
        children: [
          PhotoViewGallery.builder(
            itemCount: widget.imageList.length,
            builder: (context, index) {
              return PhotoViewGalleryPageOptions(
                imageProvider: AssetImage(widget.imageList[index]),
                minScale: PhotoViewComputedScale.contained * 0.8,
                maxScale: PhotoViewComputedScale.covered * 2,

              );
            },
            allowImplicitScrolling: true,
            scrollPhysics: const PageScrollPhysics(),
            scrollDirection: Axis.horizontal,
            reverse: false,
            backgroundDecoration: const BoxDecoration(
              color: Colors.black,
            ),
            pageController: PageController(initialPage: widget.index,keepPage: true),
            onPageChanged: (index) {
              // 处理页面切换
              setState(() {
                _currentIndex = index;
              });
            },
          ),

      Positioned(
        bottom: 20,
        child: Container(
            alignment: Alignment.center,
            width: MediaQuery.of(context).size.width,
            child: Text("${_currentIndex + 1}/${widget.imageList.length}",
                style: const TextStyle(
                  decoration: TextDecoration.none,
                  color: Colors.white,
                  fontSize: 16,
                  fontWeight: FontWeight.w400,
                  shadows: [
                    Shadow(color: Colors.black, offset: Offset(1, 1)),
                  ],
                )
              )
            ),
          ),

          Positioned(//右上角关闭
            top: 40,
            right: 40,
            child: Container(
              alignment: Alignment.centerLeft,
              width: 20,
              child: GestureDetector(
                onTap: () {
                  //隐藏预览
                  Navigator.pop(context);
                },
                child: Icon(Icons.close_rounded, color: Colors.white),
              ),
            ),
          ),
          Positioned(//数量显示
            right: 20,
            bottom: 20,
            child: GestureDetector(
              onTap: (){
                CommonUtil().saveLocalImage(_globalKey, widget.imageList[_currentIndex]);
              },
              child:  Icon(Icons.download,color: Colors.white,),
            ),
          ),
        ],
      ),
    );
  }

}

